<?php namespace Home\Controller;
require 'App/Home/Function/functions.php';
use Hdphp\Controller\Controller;

//测试控制器
class IndexController extends Controller{

	protected $tpl;
	//构造函数
	public function __init()
	{
		//分配栏目
		$this->assignCat();
	}

	//分配栏目数据
	protected function assignCat()
	{
		$db = new \Home\Model\Category;
		$cat = $db->getAll();
		View::with('cat',$cat);

	}

    //动作
	public function index(){

		View::make('indexs.html');
	}

	public function lists()
	{

		if(!isset($_GET['s']))
		{
			$this->formatUrl();
		}
		// //分配列表页用到的搜索属性
		$this->assignCateogrySearchAttr();

		// //分配列表页商品
		$this->assignCategoryGoodsList();

		View::make('lists.html');
	}

		//分配列表页商品
	private function assignCategoryGoodsList()
	{
		//搜索属性
		$s = explode('-',$_GET['s']);

		$s = array_filter($s);

		if(empty($s))
		{
			//没有选择属性的情况
			$goodsCount=Db::table('shop_goods')->where('cat_id',$_GET['cat_id'])->count();
			$page = Page::row(5)->make($goodsCount);
			$goods = Db::table('shop_goods')->where('cat_id',$_GET['cat_id'])->limit(Page::limit())->get();
			View::with('page',$page);
			View::with('goods',$goods);
		}
		else
		{
			//按用户筛选的属性检索

			$searchAttr = "'".implode("','", $s)."'";
			// $sql ="SELECT count(*) as c FROM ((SELECT * FROM hd_shop_goods_attr WHERE attr_values IN(".$searchAttr.") GROUP BY goods_id HAVING count(*)=".count($s).") AS g)";
			$count ="SELECT * FROM hd_shop_goods g JOIN hd_shop_goods_attr ga ON g.goods_id =ga.goods_id WHERE attr_values IN(".$searchAttr.") AND g.cat_id = ".$_GET['cat_id']." GROUP BY ga.goods_id HAVING count(*)=".count($s)." LIMIT ".Page::limit();

			$d = Db::select($count);

			$page = Page::row(2)->make(count($d));

			//获取商品
			$sql ="SELECT * FROM hd_shop_goods g JOIN hd_shop_goods_attr ga ON g.goods_id =ga.goods_id WHERE attr_values IN(".$searchAttr.") AND g.cat_id = ".$_GET['cat_id']." GROUP BY ga.goods_id HAVING count(*)=".count($s)." LIMIT ".Page::limit();
			$goods = Db::select($sql);
			View::with('page',$page);
			View::with('goods',$goods);
		}
	}

	//如果GET中不存在s变量，生成带有s搜索属性的变量并跳转
	private function formatUrl()
	{
		$cat = Db::table('shop_cat')->where('cat_id',$_GET['cat_id'])->first();
			//获取栏目的类型属性
		$attr = Db::table('shop_attr')->where('shop_type_id',$cat['type_id'])->get();

		$s = '&s=';

		for($i=0;$i<count($attr);$i++)
		{
			$s.='0-';
		}
		$s= substr($s,0,-1);

		$url =  __URL__.$s;

		go($url);

	}

	//分配列表页搜索属性
	private function assignCateogrySearchAttr()
	{
		$cat = Db::table('shop_cat')->where('cat_id',$_GET['cat_id'])->first();
		//获取栏目的类型属性
		$attr = Db::table('shop_attr')->where('shop_type_id',$cat['type_id'])->get();
		// p($attr);
		foreach($attr as $k=>$v)
		{
			$attr[$k]['attr_value']
			=Db::table('shop_goods_attr')
			->where('attr_id',$v['attr_id'])
			->groupBy('attr_values')
			->get();
			// p($attr[$k]['attr_value']);
		}
		// p($attr);
		View::with('searchAttr',$attr);
	}


	public function focus()
	{
		if (!isset($_SESSION['user_id'])) {
			View::ajax(array('code'=>0,'message'=>'请先登录'));
			return false;
		}
		else
		{
			$db = Db::table('shop_focus')->where('goods_id',$_POST['goods_id'])->where('user_id',$_SESSION['user_id'])->first();
			if ($db) {
				$focus['history_id']=$db['focus_id'];
				$focus['goods_id']=$_POST['goods_id'];
				$focus['user_id']=$_SESSION['user_id'];
				$focus['create_at']=time();
				if (Db::table('shop_history')->where('focus_id',$db['focus_id'])->update($focus)) {
					View::ajax(array('code'=>0,'message'=>'添加关注成功'));
				}
			}
			else
			{
				$focus['goods_id']=$_POST['goods_id'];
				$focus['user_id']=$_SESSION['user_id'];
				$focus['create_at']=time();
				if (Db::table('shop_focus')->insert($focus)) {
					View::ajax(array('code'=>0,'message'=>'添加关注成功'));
				}
			}
		}
	}

	public function item()
	{
		$his = array();
		if (isset($_SESSION['user_id'])) {
			$db = Db::table('shop_history')->where('goods_id',$_GET['goods_id'])->where('user_id',$_SESSION['user_id'])->first();
			if ($db) {
				$his['history_id']=$db['history_id'];
				$his['goods_id']=$_GET['goods_id'];
				$his['user_id']=$_SESSION['user_id'];
				$his['create_at']=time();
				Db::table('shop_history')->where('history_id',$db['history_id'])->update($his);
			}
			else
			{
				$his['goods_id']=$_GET['goods_id'];
				$his['user_id']=$_SESSION['user_id'];
				$his['create_at']=time();
				Db::table('shop_history')->insert($his);
			}

		}

		$pic = Db::table('shop_pics')->where('goods_id',$_GET['goods_id'])->first();
		View::with('pic',$pic);

		$good = Db::table('shop_goods g')
		->join('shop_brand b','g.brand_id','=','b.id')
		->where('goods_id',$_GET['goods_id'])
		->first();
		View::with('good',$good);
		$attrs = Db::table('shop_goods_attr ga')
		->join('shop_attr a','ga.attr_id','=','a.attr_id')
		->where('goods_id',$_GET['goods_id'])
		->get();

		$group = Db::table('shop_goods_attr ga')
		->join('shop_attr a','ga.attr_id','=','a.attr_id')
		->where('goods_id',$_GET['goods_id'])
		->where('a.attr_type',2)
		->groupBy('a.attr_id')
		->get();
		// p($group);
		$groups = array();
		foreach ($group as $k => $v) {
			$attr = array();
			foreach ($attrs as $x => $y) {
				if ($v['attr_id']==$y['attr_id']) {
					$attr[]	= $y;
				}
				else
				{
					continue;
				}
			}
			$groups[] = $attr;
		}

		View::with('attrs',$groups);

		// p($attrs);
		View::make('item.html');
	}

	public function checkStore()
	{
		$data = explode('_',$_POST['data']);
		$good = Db::table('shop_store')
		->where('goods_id',$_POST['goods_id'])
		->where('mix_id',$_POST['data'])
		->first();

		print_r($good['store_num']);

	}

	public function addPrice()
	{
		// p($_POST);
		$p = explode('-',$_POST['goodattrid']);
		// p($p);
		$prices = 0;
		foreach ($p as $key => $value) {
			$price = Db::table('shop_goods_attr')->where('good_attr_id',$value)->first();

			$prices += $price['add_price']*1;
		}
		print_r($prices);
	}

	public function shopcar()
	{

		$model = new \Home\Model\Category;

		if ($model->shopCar()) {

			View::make('shopcarJump.html');

		}

	}

	public function goShopCart()
	{
		$goods = array();
		$_SESSION['shopcar']=isset($_SESSION['shopcar'])?$_SESSION['shopcar']:array();
		foreach ((array)$_SESSION['shopcar'] as $a => $b) {
			$pic = Db::table('shop_pics')->where('goods_id',$b['goods_id'])->pluck('small_pic');
			$good = Db::table('shop_goods')->where('goods_id',$b['goods_id'])->first();
			$shopstore = Db::table('shop_store')->where('mix_id',$b['mix_id'])->first();
			$attrs = array();
			$mix_id = $b['mix_id'];
			$prices = $good['shop_price'];
			foreach (explode("_",$b['mix_id']) as $c => $d) {
				$attr = Db::table('shop_goods_attr ga')
				->join('shop_attr a','ga.attr_id','=','a.attr_id')
				->where('ga.good_attr_id',$d)
				->first();

				$attrs[]=$attr;
				$prices+=$attr['add_price'];
			}
			$goods[$a]['pic']=$pic;
			$goods[$a]['good']=$good;
			$goods[$a]['shopstore']=$shopstore;
			$goods[$a]['attrs']=$attrs;
			$goods[$a]['mix_id']=$mix_id;
			$goods[$a]['prices']=$prices;
			$goods[$a]['buynum']=$b['buynum'];
		}


		$_SESSION['preweb'] = 0;
		// p($goods);
		// p($_SESSION);
		View::with('goods',$goods);
		View::make('shopcar.html');

	}

	public function num()
	{

		foreach ($_SESSION['shopcar'] as $k => $v) {
			if($v['mix_id']==$_POST['mix_id'])
			{
				$_SESSION['shopcar'][$k]['buynum']=$_POST['num'];
			}
		}
	}

	public function delGood()
	{
		foreach ($_SESSION['shopcar'] as $k => $v) {
			if($v['mix_id']==$_POST['mix_id'])
			{
				unset($_SESSION['shopcar'][$k]);
				echo 1;
			}
		}
	}




}
